<template>
  <div class="login">
    <!-- 导航栏 -->
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.phone"
        name="phone"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[
        { required: true, message: '请输入手机号' },
        { validator: phoneCheck, message: '格式有误,请重新输入'}
        ]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[
        { required: true, message: '请输入密码' },
        { pattern: /^[a-zA-Z0-9]{6,12}$/, message: '格式有误,请重新输入' }
        ]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info"
        native-type="submit" :loading="isLoading">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { NavBar, Form, Field, Button, Toast } from 'vant'
import { login } from '@/services/user'
export default {
  name: 'Login',
  components: {
    VanNavBar: NavBar,
    VanForm: Form,
    VanField: Field,
    VanButton: Button
  },
  data () {
    return {
      form: {
        phone: '',
        password: ''
      },
      // 设置是否显示加载状态
      isLoading: false
    }
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    async onSubmit () {
      this.isLoading = true
      const { data } = await login(this.form)
      // console.log(data)
      if (data.state === 1) {
        Toast.success('登录成功')
        // 将用户信息存到state中
        this.$store.commit('setUser', data.content)
        // 跳转到当前的路径
        this.$router.push(this.$route.query.redirect || '/')
      } else {
        Toast.fail('登录失败')
      }
      this.isLoading = false
    },
    phoneCheck (val) {
      return /^1\d{10}$/.test(val)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
